package cn.edu.scau.cmi.wuzewei.hibernate.client;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.wuzewei.hibernate.dao.HibernateSessionFactoryUtil;
import cn.edu.scau.cmi.wuzewei.hibernate.dao.StudentDAOByHibernate;
import cn.edu.scau.cmi.wuzewei.hibernate.dao.TeacherDAOByHibernate;
import cn.edu.scau.cmi.wuzewei.hibernate.domain.Student;
import cn.edu.scau.cmi.wuzewei.hibernate.domain.Teacher;

public class HibernateDAOUtil {
	public static Student newStudent(long id, String name) {
		Session session = HibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		
		Student student = new Student();
		student.setId(id);
		student.setName(name);
		
		StudentDAOByHibernate studentDAOByHibernate = new StudentDAOByHibernate();
		studentDAOByHibernate.save(student);
		
		transaction.commit();
		session.close();
		
		return student;
	}
	
	public static void delStudent(Student student) {
		Session session = HibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		
		StudentDAOByHibernate studentDAOByHibernate = new StudentDAOByHibernate();
		studentDAOByHibernate.delete(student);
		
		transaction.commit();
		session.close();
	}
	
	public static void modStudent(Student student) {
		Session session = HibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		
		StudentDAOByHibernate studentDAOByHibernate = new StudentDAOByHibernate();
		studentDAOByHibernate.attachDirty(student);
		
		transaction.commit();
		session.close();
	}
	
	@SuppressWarnings("rawtypes")
	public static List listStudents() {
		StudentDAOByHibernate studentDAOByHibernate = new StudentDAOByHibernate();
		return studentDAOByHibernate.findAll();
	}
	
	public static Teacher newTeacher(long id, String name) {
		Session session = HibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		
		Teacher teacher = new Teacher();
		teacher.setId(id);
		teacher.setName(name);
		
		TeacherDAOByHibernate teacherDAOByHibernate = new TeacherDAOByHibernate();
		teacherDAOByHibernate.save(teacher);
		
		transaction.commit();
		session.close();
		
		return teacher;
	}
	
	public static void delTeacher(Teacher teacher) {
		Session session = HibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		
		TeacherDAOByHibernate teacherDAOByHibernate = new TeacherDAOByHibernate();
		teacherDAOByHibernate.delete(teacher);
		
		transaction.commit();
		session.close();
	}
	
	public static void modTeacher(Teacher teacher) {
		Session session = HibernateSessionFactoryUtil.getSession();
		Transaction transaction = session.beginTransaction();
		
		TeacherDAOByHibernate teacherDAOByHibernate = new TeacherDAOByHibernate();
		teacherDAOByHibernate.attachDirty(teacher);
		
		transaction.commit();
		session.close();
	}
	
	@SuppressWarnings("rawtypes")
	public static List listTeachers() {
		TeacherDAOByHibernate teacherDAOByHibernate = new TeacherDAOByHibernate();
		return teacherDAOByHibernate.findAll();
	}
	
	public static Teacher findTeacher(Teacher teacher) {
		TeacherDAOByHibernate teacherDAOByHibernate = new TeacherDAOByHibernate();
		return teacherDAOByHibernate.findById(teacher.getId());
	}
}
